package com.ruoyi.framework.pool;

import com.jcraft.jsch.Session;
import com.ruoyi.system.domain.THost;
import com.ruoyi.system.pool.Executor;
import com.ruoyi.system.pool.ShellExecutor;
import com.ruoyi.system.pool.SshPoolProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.KeyedPooledObjectFactory;
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;

/**
 * @description: ssh连接池
 * @author: stone
 * @date: Created by 2022/4/16 23:18
 * @version: 1.0.0
 * @pakeage: com.ruoyi.framework.pool
 */
@Slf4j
public class SshConnectPool extends GenericKeyedObjectPool<THost, Session> {

    public SshConnectPool(KeyedPooledObjectFactory<THost, Session> factory) {
        super(factory);
    }

    public SshConnectPool(KeyedPooledObjectFactory<THost, Session> factory, GenericKeyedObjectPoolConfig<Session> config) {
        super(factory, config);
    }

    public static SshConnectPool initPool() {
        SshConnectFactory factory = new SshConnectFactory();
        SshPoolProperties sshPoolProperties = new SshPoolProperties();
        GenericKeyedObjectPoolConfig<Session> config = new GenericKeyedObjectPoolConfig<>();
        // 多个key组 总最大数
        config.setMaxTotal(sshPoolProperties.getMaxTotalKey() * 6);
        // 一个key组 最大数
        config.setMaxTotalPerKey(sshPoolProperties.getMaxTotalKey());
        // 一个key组 最大空闲数
        config.setMaxIdlePerKey(sshPoolProperties.getMaxIdleKey());
        // 一个key组 最小空闲数
        config.setMinIdlePerKey(sshPoolProperties.getMinIdleKey());
        config.setBlockWhenExhausted(true);
        config.setTestOnReturn(true);
        config.setTestOnBorrow(true);
        config.setTestWhileIdle(true);
        config.setTimeBetweenEvictionRunsMillis(1000L * 60 * 30);
        config.setJmxEnabled(false);

        return new SshConnectPool(factory, config);
    }

    public static void main(String[] args) throws Exception {
        THost t1 = new THost();
        t1.setHostname("123.57.157.229");
        t1.setUsername("root");
        t1.setPort(8088);
        t1.setPasswd("123456");
        SshConnectPool pool = SshConnectPool.initPool();

        //pool.addObjects(t1,10);


        THost t2 = new THost();
        t2.setHostname("12");
        t2.setUsername("root");


        Session session = pool.borrowObject(t1);
        //
        //TimeUnit.SECONDS.sleep(2);
        //
        //Session session1 = pool.borrowObject(t1);

        Executor<String> executor = new ShellExecutor(session);

        String df = executor.execute("df");
        log.error("输出结果 ====> {}", df);
        //log.info("两个对象是否相对 ====>{}", t1.equals(t2));
        //
        //log.info("地址 {} => {}",session,session1);
        //log.info("两个对象是否相对1 ====>{}", session1.equals(session));

        log.error("对象池中的总数 ===> {}", pool.getMaxTotal());
        log.error("对象池创建的个数 ===> {}", pool.getCreatedCount());
        log.error("对象池中的Borrowed数 ===> {}", pool.getBorrowedCount());
        log.error("对象池中的活跃数 ===> {}", pool.getNumActive());
        log.error("对象池中的key活跃数 ===> {}", pool.getNumActive());
        log.error("对象池中的key1活跃数 ===> {}", pool.getNumActive(t1));
        log.error("对象池中的key2活跃数 ===> {}", pool.getNumActive(t2));
        log.error("对象池中的空闲数 ===> {}", pool.getMaxIdlePerKey());
        log.error("对象池中的 一个key组 最大数数 ===> {}", pool.getMaxTotalPerKey());
        //log.error("对象池中的 一个key组 最大数数 ===> {}",pool.get());

        //session.disconnect();
        pool.close();

    }
}
